import { Router } from 'express';
import { ExportController } from '../controllers/exportController';
import { authenticate } from '../middleware/auth';

const router = Router();

/**
 * @swagger
 * tags:
 *   name: 数据导出
 *   description: 数据导出和备份相关接口
 */

/**
 * @swagger
 * /api/export/user-data:
 *   get:
 *     summary: 导出用户所有数据
 *     tags: [数据导出]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: format
 *         schema:
 *           type: string
 *           enum: [json, csv]
 *         description: 导出格式
 *         example: json
 *     responses:
 *       200:
 *         description: 导出成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 user:
 *                   type: object
 *                 vehicles:
 *                   type: array
 *                 expenses:
 *                   type: array
 *                 incomes:
 *                   type: array
 *                 fuelRecords:
 *                   type: array
 *                 maintenanceRecords:
 *                   type: array
 *                 repairRecords:
 *                   type: array
 *                 chargingRecords:
 *                   type: array
 *                 exportDate:
 *                   type: string
 *                 totalRecords:
 *                   type: object
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/user-data', authenticate, ExportController.exportUserData);

/**
 * @swagger
 * /api/export/date-range:
 *   get:
 *     summary: 导出指定时间范围的数据
 *     tags: [数据导出]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: startDate
 *         schema:
 *           type: string
 *           format: date
 *         required: true
 *         description: 开始日期
 *         example: "2024-01-01"
 *       - in: query
 *         name: endDate
 *         schema:
 *           type: string
 *           format: date
 *         required: true
 *         description: 结束日期
 *         example: "2024-12-31"
 *       - in: query
 *         name: dataType
 *         schema:
 *           type: string
 *           enum: [all, expenses, incomes, fuel, maintenance, repair, charging]
 *         description: 数据类型
 *         example: all
 *       - in: query
 *         name: format
 *         schema:
 *           type: string
 *           enum: [json, csv]
 *         description: 导出格式
 *         example: json
 *     responses:
 *       200:
 *         description: 导出成功
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/date-range', authenticate, ExportController.exportDataByDateRange);

/**
 * @swagger
 * /api/export/expense-report:
 *   get:
 *     summary: 导出费用统计报告
 *     tags: [数据导出]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: year
 *         schema:
 *           type: integer
 *         description: 年份
 *         example: 2024
 *       - in: query
 *         name: month
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 12
 *         description: 月份
 *         example: 12
 *       - in: query
 *         name: format
 *         schema:
 *           type: string
 *           enum: [json, csv]
 *         description: 导出格式
 *         example: json
 *     responses:
 *       200:
 *         description: 导出成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 period:
 *                   type: object
 *                   properties:
 *                     startDate:
 *                       type: string
 *                     endDate:
 *                       type: string
 *                 totalExpenses:
 *                   type: integer
 *                 totalAmount:
 *                   type: number
 *                 expenseStats:
 *                   type: object
 *                 vehicleStats:
 *                   type: object
 *                 expenses:
 *                   type: array
 *                 exportDate:
 *                   type: string
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get(
  '/expense-report',
  authenticate,
  ExportController.exportExpenseReport
);

/**
 * @swagger
 * /api/export/backup:
 *   post:
 *     summary: 创建数据备份
 *     tags: [数据导出]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 备份创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                 backup:
 *                   type: object
 *                   properties:
 *                     version:
 *                       type: string
 *                     backupDate:
 *                       type: string
 *                     user:
 *                       type: object
 *                     data:
 *                       type: object
 *                     summary:
 *                       type: object
 *                 downloadUrl:
 *                   type: string
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.post('/backup', authenticate, ExportController.createBackup);

export default router;
